package edu.mit.media.fv.components
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class FVComboBox extends Sprite
	{
		private static var MARGIN:int = 6;
		
		private var _glow:GlowFilter = new GlowFilter(0xFFFFFF,0.8,15,15);
		private var _bevel:BevelFilter = new BevelFilter(
			4, 45, 0xFFFFFF, 1, 0, 1, 4, 4, 1, 1);
		private var _width:int;
		private var _height:int;
		private var _cellHeight:int;
		private var _field:TextField;
		private var _format:TextFormat;
		private var _backwall:Sprite;
		private var _active:Boolean;
		private var _menu:Sprite;
		private var _mask:Sprite;
		private var _items:Array;
		private var _timer:Timer;
		private var count:int = 0;
		private var _selectedIndex:int = -1;
		
		public function FVComboBox(x:int = 0, y:int = 0, width:int = 100, height:int = 28){
			super();
			this._active = false;
			
			this._timer = new Timer(25);
			
			this._menu = new Sprite();
			this._menu.x = MARGIN;
			this.addChild(this._menu);
			
			this._backwall = new Sprite();
			this._backwall.filters = [this._bevel];
			this.addChild(this._backwall);
			
			this._format = new TextFormat();
			this._format.align = TextFormatAlign.LEFT;
			this._format.bold = true;
			this._format.font = "Arial";
			this._format.size = this._height*0.8;
			this._format.color = 0x000000;
			
			this._field = new TextField();
			this._field.defaultTextFormat = _format;
			this._field.type = TextFieldType.DYNAMIC;
			this._field.selectable = false;
			this.addChild(this._field);
			
			this._mask = new Sprite();
			this._mask.graphics.clear();
			this._mask.graphics.beginFill(0x000000);
			this._mask.graphics.drawRect(0,0, this._width+2, height);
			this._mask.graphics.endFill();
			this.addChild(this._mask);
			this.mask = this._mask;
			
			
			this._items = new Array();
			
			this.setBounds(x, y, width, height);
			
			this.addEventListener(MouseEvent.CLICK, processMouseClicked);
			//this.addEventListener(FocusEvent.FOCUS_OUT, processFocusOut);
			this._timer.addEventListener(TimerEvent.TIMER, processTimerOn);
			
			
		}
		
		public function setBounds(x:int, y:int, width:int, height:int):void{
			this._width = width;
			this._height = height;
			this._cellHeight = height-MARGIN*2;
			this.x = x;
			this.y = y;
			this._field.width = this._width - this._height-MARGIN*3;
			this._field.height = this._height- MARGIN*2;
			this._field.x = MARGIN;
			this._field.y = MARGIN;
			this._format.size = this._height*0.5;
			this._field.defaultTextFormat = this._format;
			this.draw();
		}
		private function draw():void{
			var arc:int = 10;
			var w:int = this._width;
			var h:int = this._height;
			var m:int = MARGIN;
			
			this._backwall.graphics.clear();
			
			if(_active){
				this._backwall.graphics.beginFill(0xCCCCCC);
				this._backwall.graphics.lineStyle(1, 0xFFFFFF, 1.0, true);
			}else{
				this._backwall.graphics.beginFill(0x888888);
				this._backwall.graphics.lineStyle(1, 0xFFFFFF, 1.0, true);
			}
			this._backwall.graphics.drawRoundRect(0,0,w,h,10,10);
			//this._backwall.graphics.drawRoundRect(m, m,w-h-m,h-2*m,arc,arc);
			//this._backwall.graphics.drawRoundRect(w-h+m, m, h-2*m, h-2*m, arc, arc);
			//this._backwall.graphics.lineStyle(2, 0xFFFFFF);
			this._backwall.graphics.beginFill(0x000000);
			this._backwall.graphics.moveTo(w-h+2*m, 2*m);
			this._backwall.graphics.lineTo(w-h/2, h-2*m);
			this._backwall.graphics.lineTo(w-2*m, 2*m);
			this._backwall.graphics.endFill();
		}
		//takes in array of strings
		public function setItems(items:Array):void{
			this._mask.graphics.clear();
			this._mask.graphics.beginFill(0x000000);
			this._mask.graphics.drawRect(0,0, this._width+2, this._height*(items.length+1));
			this._mask.graphics.endFill();
			
			this._items = new Array();
			for(var i:int = 0; i<items.length; i++){
				var item:FVCell = new FVCell(
					1,this._cellHeight*i+1,
					this._width-MARGIN*2-2,this._cellHeight-2,
					i,items[i].toString(),
					this.itemSelected);
				this._menu.addChild(item);
				this._items.push(item);
			}
			
			this._menu.graphics.clear();
			this._menu.graphics.beginFill(0x000000, 0.9);
			this._menu.graphics.lineStyle(1.0, 0xFFFFFF, 1.0, true);
			this._menu.graphics.drawRoundRectComplex(
				0,0,
				this._width-MARGIN*2, this._cellHeight*this._items.length,
				0,0,
				3,3);
			this._menu.graphics.endFill();
			
			if (items.length > 0){
				this._field.text = items[0].toString();
				this._selectedIndex = 0;
			}
			this._active = false;	
			this._menu.y = -1*this._cellHeight*this._items.length;
		}
		private function itemSelected(event:Event):void{
			var s:FVCell = FVCell(event.target);
			this._field.text = s.text;
			this._selectedIndex = s.index;
			this.dispatchEvent(new FVChangeEvent(FVChangeEvent.VALUE_CHANGED, this._selectedIndex));
		}
		public function get selectedIndex():int{
			return this._selectedIndex;
		}
		public function set selectedIndex(index:int):void{
			this._field.text = FVCell(this._items[index]).text;
			this._selectedIndex = index;
		}
		public function processFocusOut(event:FocusEvent):void{
			count = 0;
			this._active = false;
			this.draw();
			this._timer.start();
		}
		public function processMouseClicked(event:MouseEvent):void{
			count = 0;
			this._active = !this._active;
			this.draw();
			this._timer.start();
		}
		private function processTimerOn(event:TimerEvent):void{
			var totalCount:int = 5;
			if(count>totalCount){
				if(this._active){
					this._menu.y = this._height;
					this._timer.reset();
				}else{
					this._menu.y = -1*this._cellHeight*this._items.length;
					this._timer.reset();
				}
				count = 0;
			}else{
				if(this._active){
					this._menu.y = this._height+((totalCount-count)/totalCount)*-1*this._cellHeight*this._items.length;
				}else{
					this._menu.y = this._height+(count/totalCount)*-1*this._cellHeight*this._items.length;
				}
				count = count+1;
			}

		}
	}
}